/**
 * 
 */
package com.gr.staffpm.pages.notes;

import java.util.ArrayList;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Instant;
import org.joda.time.Period;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import com.gr.staffpm.datatypes.Activity;
import com.gr.staffpm.model.StringModel;
import com.gr.staffpm.notes.NotificationPanel;
import com.gr.staffpm.notes.activity.ActivityMessage;


/**
 * @author Graham Rhodes 24 Feb 2011 19:01:02
 */
public class GroupNotesPanel extends Panel implements Comparable<GroupNotesPanel> {

    private static final long serialVersionUID = 1L;
    private final DateTime activitiesDate;

    public GroupNotesPanel(String id, DateTime activitiesDate, ArrayList<Activity> activities) {
        super(id);
        this.activitiesDate = activitiesDate;

        Label groupNotesTitle = new Label("groupNotesTitle", new StringModel(""));
        Period diff = new Period(activitiesDate, new Instant());
        DateTimeFormatter fmt;

        if (diff.toStandardHours().isLessThan(Hours.hours(24))) {
            groupNotesTitle.setDefaultModel(new StringModel("Today"));
        } else if (diff.toStandardDays().isLessThan(Days.SEVEN)) {
            fmt = DateTimeFormat.forPattern("EEEEE");
            groupNotesTitle.setDefaultModel(new StringModel(fmt.print(activitiesDate)));
        } else {
            fmt = DateTimeFormat.forPattern("dd MMMM yyyy");
            groupNotesTitle.setDefaultModel(new StringModel(fmt.print(activitiesDate)));
        }
        add(groupNotesTitle);

        RepeatingView notificationItems = new RepeatingView("notificationItems");
        for (Activity act : activities) {
            ActivityMessage activityMessage = new ActivityMessage(act.getTask(), new DateTime(act.getCreated()), com.gr.staffpm.notes.activity.Activity.parse(act));
            notificationItems.add(new NotificationPanel(String.valueOf(act.getActivityId()), activityMessage));
        }
        add(notificationItems);

    }

    public DateTime getActivitiesDate() {
        return activitiesDate;
    }

    /* (non-Javadoc)
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    @Override
    public int compareTo(GroupNotesPanel other) {
        return this.activitiesDate.compareTo(other.getActivitiesDate());
    }

}
